<?php
	session_start(); //Bắt đầu SESSION
	ob_start();	//Mở output buffer
    error_reporting(E_ALL);


	require("data/global_config.php");//File cấu hình CSDL

	define('LT2N_ROOTDIR',  dirname(__FILE__)); //Thư mục gốc
	define("LT2N_BASEURL", "http://".$global['domain']."/"); //Đường dẫn URL gốc

	define("LT2N_VIEW_PATH", "views/");
	define("LT2N_THEME_PATH", "themes/");
	define("LT2N_BLOCK_PATH", "includes/blocks/");
	define("LT2N_MODULE_PATH", "modules/");
	define("LT2N_CONTROLLER_PATH", "controllers/");
	// define("LT2N_SYS", true);
	define("LT2N_HOST", $global['host']);
	define("LT2N_DBNAME", $global['dbname']);
	define("LT2N_DBUSER", $global['user']);
	define("LT2N_DBPASS", $global['pass']);
	define("LT2N_PREFIX", $global['prefix']);
    define( 'SESSION_KEY', 'OJHDM3jzMX5OjATleekrd7s8oCfpDUqb');

    define( 'ASSETS_PATH' , LT2N_BASEURL . 'assets/' );
    define( 'JS_PATH' , LT2N_BASEURL . 'js/' );
    define( 'CSS_PATH' , LT2N_BASEURL . 'assets/css/' );
    define( 'IMAGES_PATH' , LT2N_BASEURL . 'assets/images/' );

    //Users Permission
    define("PERMISSION_USER", 1);
    define("PERMISSION_MANAGER", 7);
    define("PERMISSION_ADMIN", 31);

    define("SUCCESS_DELETE"     ,2);
    define("SUCCESS_EDIT"       ,3);
    define("SUCCESS_ADD"        ,4);

    //Khởi tạo giá trị cho biến GET['mod']
    if(!isset($_GET['mod'])){
        $_GET['mod'] = '';
    }

    //Khởi tạo giá trị cho biến GET['act']
    if(!isset($_GET['act'])){
        $_GET['act'] = '';
    }
    define("LT2N_MODULENAME",$_GET["mod"]);//Khai báo hằng tên các module

	include("data/config.php");
	include("includes/nvs_sys.class.php");
	include("includes/nvs_router.php");
	include("includes/model.class.php");

    include("includes/users.class.php");
    include("includes/kiemketaisan.class.php");
    include("includes/loaitaisan.class.php");
    include("includes/danhmuctaisan.class.php");
    include("includes/search.class.php");

	include("includes/nvs_paging.class.php");


	//include("js/editor/nvs_editor.php");

	$module["name"] = $_GET["mod"];
	$module["theme"] = nvs_setTheme('home','member','default');
	$db = new Model();
	define("LT2N_THEMEURL",$module["theme"]);


    /*----------------------------------------------*
     * Functions
     *----------------------------------------------*/
    /**
     * Send mail functions
     */
	function sendMail($subject, $body, $to, $nameto)
	{
		if(global_config('use_mail') == "2")
		{
			$email = global_config('smtp_username');
			$pass = global_config('smtp_password');
			$mail = new PHPMailer();
			$mail->IsSMTP();
			$mailer->CharSet="utf8";
			$mail->Host = global_config('smtp_host');
			$mail->Port = global_config('smtp_port');
			$mail->SMTPAuth = true;
			$mail->SMTPSecure = 'ssl';
			$mail->Username = $email;
			$mail->Password = $pass;
			$from = $email;
			$to=$to;
			$name=$nameto;
			$mail->From = $from;
			$mail->FromName = $email;
			$mail->AddAddress($to,$name);
			$mail->AddReplyTo($from,"Email");
			$mail->WordWrap = 50;
			$mail->IsHTML(true);
			$mail->Subject = $subject;
			$mail->Body = $body;
			$mail->AltBody = $subject;

			if(!$mail->Send())
			{
				return 0;
			}
			else
			{
				return 1;
			}
		}
		else
		{
			mail($to,$subject,$body);
		}
	}

    /*
     * Thiết lập theme cho từng module
     * Nếu module hoặc theme không tồn tại thì sẽ trả về theme mặc định
     */
	function nvs_setTheme($module,$default,$themes="")
	{
		if(isset($_GET["mod"]))
		{
			if($module == $_GET["mod"])
			{
				if($themes != "")
				{
					return $themes;
				}
				else
				{
					return $default;
				}
			}
			else
			{
				return $default;
			}
		}
		else
		{
			return $default;
		}
	}


    /*
     * ???
     */
	function nvs_siteThemes($content)
	{
		echo $content;
	}

    /*
     * Gọi hàm
     */
	function call_function($file,$funcname)
	{
		include_once($file);
		return call_user_func("index".$funcname);
	}


	function nvs_blockThemes($content="")
	{
		$str = "";
		if(is_array($content))
		{
			foreach($content as $block)
			{
				$str .= call_function(LT2N_BLOCK_PATH."blocks.".$block.".php",$block);
			}
			return $str;
		}
	}
?>